function mismatch_list = fcnmatch(control_command, descend_flag);

% FCNMATCH - matches MATLAB M file names to the function name
%
%    FILE_LIST = FCNMATCH('find') constructs a list of MATLAB M
%    file names that do not match the corresponding function name
%    provided in the function header line (i.e., function [x, y, z]
%    = fcn_name(a, b, c)).  FILE_LIST is a structure that contains
%    the mismatched file names and the corresponding function names.
%
%    FILE_LIST = FCNMATCH('replace') replaces the mismatched file
%    names with the corresponding function names.  If no function
%    name is provided (i.e., a script), then the file name is not
%    changed.
%
%    FILE_LIST = FCNMATCH(..., 'descend') forces the file search
%    to descend subdirectories contained within the user-specified
%    parent directory.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: August 27, 1999

% global declarations

global PREV_PATH		% previous directory selected

% initialize output to prevent MATLAB errors

mismatch_list = [];

% check command line arguments

if (nargin < 1) | (nargin > 2)
   msgbox('Invalid number of function arguments', 'FCNMATCH Error', 'warn');
   return
elseif (~ischar(control_command))
   msgbox('Function arguments must be of type CHAR', 'FCNMATCH Error', 'warn');
   return
elseif (nargin == 1)
   descend_flag = 'nodescend';
elseif (~ischar(descend_flag))
   msgbox('Function arguments must be of type CHAR', 'FCNMATCH Error', 'warn');
   return
end

% have user select a filename
% only the directory is used
% maybe I'll get around to making a MEX file called UIGETDIR

if (ischar(PREV_PATH))
   if (exist(PREV_PATH) == 7)
      cd(PREV_PATH);
   end
end
[file_name, path_name] = uigetfile('*.m', 'Search Directory');
if ((file_name == 0) & (path_name == 0))
   return
else
   PREV_PATH = path_name;
end

% obtain directory listing
% descend subdirectories if specified

file_list = dirlist(path_name, descend_flag);

% determine indices of MATLAB M files
% loop through M files to read function line

file_index = find(strcmpi({file_list(:).ext}, '.m'));
for i = file_index
   [fcn_name, input_args, output_args] = fcnparse([file_list(i).path file_list(i).name file_list(i).ext]);
   
   % check for function/file name mismatch
   % do not include script or help files
   % add to output list if mismatch found
   
   if (~strcmpi(file_list(i).name, fcn_name) ...
         & ~strcmp(fcn_name, '< script >') & ~strcmp(fcn_name, '< help >'))
      mismatch_list(end + 1).path = file_list(i).path;
      mismatch_list(end).name = file_list(i).name;
      mismatch_list(end).ext = file_list(i).ext;
      mismatch_list(end).fcn = fcn_name;
      mismatch_list(end).input = input_args;
      mismatch_list(end).output = output_args;
   end
end
return
